package com.coastwe.dahua.chapter04;

public class LinkStack {
    private Node head;

    public LinkStack() {
        this.head = null;
    }

    public void push(int value) {
        Node p = createNode(value);
        p.next = head;
        head = p;
    }

    public int pop() {
        if (head == null) {
            System.out.println("堆栈已经空了");
        }
        int value = head.data;
        head = head.next;
        return value;
    }

    public static void main(String[] args) {
        LinkStack stack = new LinkStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
    }

    private static Node createNode(int value) {
        return new Node(value, null);
    }

    private static class Node {
        int data;
        Node next;

        public Node(int value, Node next) {
            this.data = value;
            this.next = next;
        }
    }
}